package com.topcent.tioxyc.consumer;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import com.topcent.tioxyc.constants.KeyConstant;
import com.topcent.tioxyc.pojo.*;
import com.topcent.tioxyc.util.ApiClient;
import com.topcent.tioxyc.util.ConnmeUtils;
import com.topcent.tioxyc.util.PurchaseCommunity;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: weidian
 * @date: 2024/10/9 10:03
 * @Description: 这是一个采购订单消费类
 */
@Component
@RabbitListener(queues = "queue_a")
@Slf4j
public class PurchaseOrderConsumer {

    private static final String DATA_CREATE = "data_create";
    private static final String DATA_UPDATE = "data_update";
    private static final String DATA_DELETE = "data_remove";

    @RabbitHandler
    public void handleMessage(String msg, Channel channel, Message message) throws IOException {
        try {
            // 处理 payload 消息
            //System.out.println("Received payload: " + msg);
            // 在这里执行具体的业务逻辑

            // 在这里处理推送来的数据
            JSONObject payloadJSON = JSONUtil.parseObj(msg);
            String op = payloadJSON.getStr("op");
            String data = payloadJSON.getStr("data");

            if (DATA_CREATE.equals(op)) {
                create(data);
            }else if (DATA_UPDATE.equals(op)){
                update(data);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("Message acknowledged");
        } catch (Exception e) {
            // 处理异常并拒绝消息
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            System.err.println("发送消息失败，重试中 " + e.getMessage());
        }
    }
    private void create(String data) {
        PurchaseOrder purchaseOrder = JSONUtil.toBean(data, PurchaseOrder.class);
        Header[] headers = ConnmeUtils.createHeaders();
        int o=10;
        for (PurchaseOrder.PurchaseOrderItem purchaseOrderItem : purchaseOrder.get_widget_1720764671480()) {
            purchaseOrderItem.set_widget_1727255069874(o);
            if (purchaseOrderItem.get_widget_1732608409521()!=null){
                purchaseOrderItem.set_widget_1732608409521(purchaseOrderItem.get_widget_1732608409521());
            }
            o+=10;
            Map<String, Object> shenqing = new HashMap<>();
            shenqing.put("app_id", "6690c7633df0f4b90b4ef551");
            shenqing.put("entry_id", "66fa4b35f0b9ba61d818e3a5");
            HashMap<String, String > shenqingdan=new HashMap<>();
            shenqingdan.put("_widget_1726627883580",purchaseOrderItem.get_widget_1727341213780());
            shenqingdan.put("_widget_1727679387046",purchaseOrderItem.get_widget_1727341827568());
            List<PurchaseRequest> purchaseRequests = PurchaseCommunity.queryBased(shenqing, headers,shenqingdan , PurchaseRequest.class);
            if (!purchaseRequests.isEmpty()){
                int widget1727679387052 = purchaseRequests.getFirst().get_widget_1727679387052();
                purchaseRequests.getFirst().set_widget_1727679387052(widget1727679387052-purchaseOrderItem.get_widget_1720764671522());


                Map<String, Object> parameterInster = new HashMap<>();
                parameterInster.put("app_id", "6690c7633df0f4b90b4ef551");
                parameterInster.put("entry_id", "66fa4b35f0b9ba61d818e3a5");
                parameterInster.put("is_start_trigger", "true");
                parameterInster.put("data_id", purchaseRequests.getFirst().get_id());
                purchaseRequests.getFirst().set_id(null);
                parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(purchaseRequests.getFirst()));
                String jsonSalee = JSONUtil.toJsonStr(parameterInster);
                ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);
            }
        }
        purchaseOrder.set_widget_1727343408271(purchaseOrder.get_widget_1720764671480().getFirst().get_widget_1720764671521());

        PurchaseOrder purchaseOrder1 = ConnmeUtils.updateFields(purchaseOrder);
        Map<String, Object> parameterInster = new HashMap<>();
        parameterInster.put("app_id", "6690c7633df0f4b90b4ef551");
        parameterInster.put("entry_id", "66f10e2f1c1c1dec5641cd6c");
        parameterInster.put("is_start_trigger", "true");
        parameterInster.put("data_id", purchaseOrder1.get_id());
        purchaseOrder1.set_id(null);
        parameterInster.put("data", ConnmeUtils.convertEntityToMapNull(purchaseOrder1));
        String jsonSalee = JSONUtil.toJsonStr(parameterInster);
        log.info("创建采购订单更新的数据{}", jsonSalee);
        ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalee);
    }

    private void update(String data){
        PurchaseOrder purchaseOrder = JSONUtil.toBean(data, PurchaseOrder.class);
        Header[] headers = ConnmeUtils.createHeaders();
        for (PurchaseOrder.PurchaseOrderItem purchaseOrderItem : purchaseOrder.get_widget_1720764671480()) {
            if (purchaseOrderItem.get_widget_1728524181579().equals("Y")){
                Map<String, Object> shenqing = new HashMap<>();
                shenqing.put("app_id", "6690c7633df0f4b90b4ef551");
                shenqing.put("entry_id", "66fa4b35f0b9ba61d818e3a5");
                HashMap<String, String > shenqingdan=new HashMap<>();
                shenqingdan.put("_widget_1726627883580",purchaseOrderItem.get_widget_1727341213780());
                shenqingdan.put("_widget_1727679387046",purchaseOrderItem.get_widget_1727341827568());
                List<PurchaseRequest> purchaseRequests = PurchaseCommunity.queryBased(shenqing, headers,shenqingdan , PurchaseRequest.class);
                int widget1727679387052 = purchaseRequests.getFirst().get_widget_1727679387052();
                purchaseRequests.getFirst().set_widget_1727679387052(widget1727679387052+purchaseOrderItem.get_widget_1720764671522());


                Map<String, Object> parameterInsters = new HashMap<>();
                parameterInsters.put("app_id", "6690c7633df0f4b90b4ef551");
                parameterInsters.put("entry_id", "66fa4b35f0b9ba61d818e3a5");
                parameterInsters.put("is_start_trigger", "true");
                parameterInsters.put("data_id", purchaseRequests.getFirst().get_id());
                purchaseRequests.getFirst().set_id(null);
                parameterInsters.put("data", ConnmeUtils.convertEntityToMapNull(purchaseRequests.getFirst()));
                String jsonSalees = JSONUtil.toJsonStr(parameterInsters);
                ApiClient.sendPostRequest(KeyConstant.Jian_URL + "/v5/app/entry/data/update", headers, jsonSalees);
            }
        }
    }
}
